package com.situ.store.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {

	private static Pattern linePattern = Pattern.compile("_(\\w)");
	private static Pattern humpPattern = Pattern.compile("[A-Z]");
	private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
	private static SimpleDateFormat sdfD = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 判断是否为空，是否为空串
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNotNullAndNotEmpty(String str) {
		return str != null && !"".equals(str);
	}

	/**
	 * 下划线转驼峰
	 * 
	 * @param str
	 * @return
	 */
	public static String lineToHump(String str) {
		if (str == null || str.indexOf("_") < 0) {
			return str;
		}
		str = str.toLowerCase();
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

//	/**
//	 * 驼峰转下划线(简单写法，效率低于{@link #humpToLine2(String)})
//	 */
//	public static String humpToLine(String str) {
//		return str.replaceAll("[A-Z]", "_$0").toLowerCase();
//	}

	/**
	 * 驼峰转下划线,效率比上面
	 */
	public static String humpToLine(String str) {
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	/**
	 * 字符串转换为日期（yyyy-MM-dd）
	 * 
	 * @param str
	 * @return
	 */
	public static Date parseDate(String str) {
		try {
			return sdf.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 日期（yyyy-MM-dd）转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String parseStr(Date date) {
		return sdf.format(date);
	}

	/**
	 * 字符串转换为日期（yyyy-MM-dd HH:mm:ss）
	 * 
	 * @param str
	 * @return
	 */
	public static Date parseDateTime(String str) {
		try {
			return sdfD.parse(str);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * 日期（yyyy-MM-dd HH:mm:ss）转字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String parseStrD(Date date) {
		return sdfD.format(date);
	}

	/**
	 * 判断字符串是否为空 为空赋值为空串
	 * 
	 * @param str
	 * @return
	 */
	public static String[] insertEmpty(String... str) {
		for (int i = 0; i < str.length; i++) {
			str[i] = str[i] == null ? "" : str[i];
		}
		return str;
	}

	// 正则表达式取出连续的四个数字
	public static List<String> regExpYear(String str){
		List<String> list = new ArrayList<>();
		String regStr = "\\d\\d\\d\\d";
		Pattern pattern = Pattern.compile(regStr);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()){
			list.add(matcher.group(0));
		}
		return list;
	}


	public static List<String>regExpMonth(String str) {
	    List<String> list = new ArrayList<>();
		String regStr = "\\d\\d\\d\\d-\\d\\d";
		Pattern pattern = Pattern.compile(regStr);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()){
			list.add(matcher.group(0));
		}
		return list;
	}


	public static List<String> regExpDay(String str) {
		List<String> list = new ArrayList<>();
		String regStr = "\\d\\d\\d\\d-\\d\\d-\\d\\d";
		Pattern pattern = Pattern.compile(regStr);
		Matcher matcher = pattern.matcher(str);
		while (matcher.find()){
			list.add(matcher.group(0));
		}
		return list;
	}
}
